<template>
  <div class="beds-list">
    <div class="beds-list-header">
      <div class="beds-list-from">
        <DynamicForm
          ref="dynamicFormRef"
          type="search"
          :rulesForm="state.searchData"
          :refresh="state.refresh"
          @click="callBack"
        />
      </div>
      <div class="beds-list-button">
        <el-button
          type="primary"
          @click="
            () => {
              toBedRouter();
            }
          "
          >新增</el-button
        >
      </div>
    </div>
    <div class="beds-list-table">
      <DynamicTable
        ref="dynamicTableRef"
        :columns="headerColumns"
        :table="state.table"
        :data="state.data"
        @click="callBack"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted, reactive } from "vue";
import DynamicForm from "com/layout/form/dynamic-form.vue";
import DynamicTable from "com/layout/table/dynamic-table.vue";
import { headerColumns } from "@/assets/script/middle/columns/system_user";

import { searchData } from "@/assets/script/middle/forms/system/user/search-form";
import {
  findRmsUserPageListApi,
  modifyPasswordApi,
  modifyRmsUserApi,
} from "@/api/system";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";

const dynamicFormRef = ref(DynamicForm);
const dynamicTableRef = ref(DynamicTable);
const router = useRouter();
const state = reactive({
  data: [],
  table: {
    total: 0,
    pageSize: 15,
    pageIndex: 1,
  },
  searchData: searchData as any,
  refresh: 0,
});
onMounted(() => {
  findRmsUserPageListFun({
    pageIndex: 1,
    pageSize: 15,
  });
});

const callBack = (data: any) => {
  console.log(data.type);
  switch (data.type || "") {
    //选择是否禁用
    case "switch":
      modifyRmsUserApi(data.data).then((data) => {
        if (data.code == 0) {
          ElMessage({
            type: "success",
            message: "修改成功",
          });
          findRmsUserPageListFun({
            pageIndex: 1,
            pageSize: 15,
          });
        }
      });
      break;
    //编辑
    case "edit":
      router.push({ path: "/system/user/edit", query: { id: data.data.id } });
      break;
    //重置密码
    case "reset":
      ElMessageBox.confirm("确认要重置密码?", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          modifyPasswordApi({
            id: data.data.id,
            oldPassword: "000000",
          }).then((data) => {
            if (data.code == 0) {
              ElMessage({
                type: "success",
                message: "重置密码成功",
              });
              findRmsUserPageListFun({
                pageIndex: 1,
                pageSize: 15,
              });
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消操作",
          });
        });
      break;
    //删除
    case "delete":
      ElMessageBox.confirm("确认要删除?", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = data.data;
          params.deleted = 1;
          modifyRmsUserApi(params).then((data) => {
            if (data.code == 0) {
              ElMessage({
                type: "success",
                message: "删除成功",
              });
              findRmsUserPageListFun({
                pageIndex: 1,
                pageSize: 15,
              });
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消操作",
          });
        });

      break;

    //查询表单
    case "submit":
      findRmsUserPageListFun({
        pageIndex: 1,
        pageSize: 15,
        loginName: data.data.loginName, //用户名
        displayName: data.data.displayName, //姓名
        cellphone: data.data.cellphone, //手机号
      });
      break;
    //重置表单
    case "rest":
      state.searchData = searchData;
      state.table.pageIndex = 1;
      state.table.pageSize = 15;
      findRmsUserPageListFun({
        pageIndex: 1,
        pageSize: 15,
      });
      break;
    case "page":
      if (!state.searchData) {
        state.searchData = {};
      }
      state.searchData["pageIndex"] = data.data;
      state.searchData["pageSize"] = state.table.pageSize;
      state.table.pageIndex = data.data;
      findRmsUserPageListFun({
        pageIndex: data.data,
        pageSize: state.table.pageSize,
      });
      break;
    case "pageSize":
      if (!state.searchData) {
        state.searchData = {};
      }
      state.searchData["pageIndex"] = state.table.pageIndex;
      state.searchData["pageSize"] = data.data;
      state.table.pageSize = data.data;
      findRmsUserPageListFun({
        pageIndex: data.data,
        pageSize: state.table.pageSize,
      });
      break;
  }
};
//获取列表数据方法
const findRmsUserPageListFun = (params: any) => {
  params.userTypeIdList = [
    "nurse",
    "nursing_workers",
    "super",
    "reception",
    "stock",
    "doctor",
    "finance",
    "admin",
    "customer",
  ];
  (params.deleted = 0),
    findRmsUserPageListApi(params).then((data) => {
      if (data.code == 0) {
        if (data.data.length != 0) {
          state.data = data.data;
        } else {
          state.data = [];
        }
        state.table.total = data.total;
        state.table.pageIndex = data.pageIndex;
      }
    });
};
//新增
const toBedRouter = () => {
  router.push({ path: "/system/user/add" });
};
</script>
